﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebEntities;
using WebEntities.Models.Framework;

namespace com.subb.WebFramework
{
    public class FW_MenuItemDAL
    {
        public List<FW_MenuItem> GetAllMenuItems(int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.FW_MenuItem.OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.ItemID);
                ttlCount = query.Count();
                List<FW_MenuItem> itemlist = query.Skip(start).Take(limit).ToList();
                return itemlist;
            }
        }

        public List<FW_MenuItem> GetAllMenuItems()
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.FW_MenuItem.OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.ItemID).AsQueryable();
                List<FW_MenuItem> itemlist = query.ToList();
                return itemlist;
            }
        }

        public List<UIMenuItem> GetAllUIMenuItems(int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                var query = ctx.FW_MenuItem.OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.ItemID);
                ttlCount = query.Count();
                List<FW_MenuItem> itemlist = query.Skip(start).Take(limit).ToList();
                List<UIMenuItem> uitemlist = new List<UIMenuItem>();
                foreach (var mitem in itemlist)
                {
                    string menuName = string.Empty;
                    string pitemName = string.Empty;
                    if (mitem.FW_Menu != null)
                    {
                        menuName = mitem.FW_Menu.MenuName;
                    }
                    if (mitem.ParentID.HasValue && mitem.ParentID.Value > 0)
                    {
                        FW_MenuItem pitem = GetMenuItem(mitem.ParentID.Value);
                        if (pitem != null)
                        {
                            pitemName = pitem.ItemName;
                        }
                    }
                    UIMenuItem uitem = UIMenuItem.AsUIMenuItem(mitem, menuName, pitemName);
                    uitemlist.Add(uitem);
                }
                return uitemlist;
            }
        }

        public List<UIMenuItem> GetAllUIMenuItems()
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                var query = ctx.FW_MenuItem.OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.ItemID);
                List<FW_MenuItem> itemlist = query.ToList();
                List<UIMenuItem> uitemlist = new List<UIMenuItem>();
                foreach (var mitem in itemlist)
                {
                    string menuName = string.Empty;
                    string pitemName = string.Empty;
                    if (mitem.FW_Menu != null)
                    {
                        menuName = mitem.FW_Menu.MenuName;
                    }
                    if (mitem.ParentID.HasValue && mitem.ParentID.Value > 0)
                    {
                        FW_MenuItem pitem = GetMenuItem(mitem.ParentID.Value);
                        if (pitem != null)
                        {
                            pitemName = pitem.ItemName;
                        }
                    }
                    UIMenuItem uitem = UIMenuItem.AsUIMenuItem(mitem, menuName, pitemName);
                    uitemlist.Add(uitem);
                }
                return uitemlist;
            }
        }

        public List<FW_MenuItem> GetMenuItemsByMenuID(int menuId)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.FW_MenuItem.Where(c => c.MenuID == menuId);
                List<FW_MenuItem> itemlist = query.ToList();
                return itemlist;
            }
        }

        public List<FW_MemberMuItem> GetAllMemberMuItem(string gid, int start, int limit, out int ttlCount)
        {
            SugardbEntities ctx = new SugardbEntities();
            var query = ctx.FW_MemberMuItem.Where(c => c.GID.Equals(gid, StringComparison.CurrentCultureIgnoreCase)).OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.MuItemID);
            ttlCount = query.Count();
            List<FW_MemberMuItem> itemList = query.Skip(start).Take(limit).ToList();
            return itemList;
        }

        public List<FW_MemberMuItem> GetAllMemberMuItem(string gid)
        {
            SugardbEntities ctx = new SugardbEntities();
            var query = ctx.FW_MemberMuItem.Where(c => c.GID.Equals(gid, StringComparison.CurrentCultureIgnoreCase)).OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.MuItemID);
            List<FW_MemberMuItem> itemList = query.ToList();
            return itemList;
        }

        public FW_MenuItem GetMenuItem(int itemId)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                FW_MenuItem muitem = ctx.FW_MenuItem.SingleOrDefault(c => c.ItemID == itemId);
                return muitem;
            }
        }

        public FW_MemberMuItem GetMemberMuItem(string gid, int itemId)
        {
            SugardbEntities ctx = new SugardbEntities();
            FW_MemberMuItem memMuItem = MenuItemQuery.GetMemberMuItemQuery(ctx, gid, itemId);
            return memMuItem;
        }

        public void CreateMenuItem(string itemName, int? menuId, string url, string mvcArea, string mvcCtrl, string mvcAction, int? parentId, string creator, int? sortNum)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                FW_MenuItem muitem = new FW_MenuItem
                {
                    ItemName = itemName,
                    MenuID = menuId == 0 ? null : menuId,
                    ItemUrl = url,
                    MvcArea = mvcArea,
                    MvcCtrl = mvcCtrl,
                    MvcAction = mvcAction,
                    ParentID = parentId == 0 ? null : parentId,
                    CreateTime = DateTime.Now,
                    Creator = creator,
                    SortNum = sortNum
                };
                ctx.FW_MenuItem.AddObject(muitem);
                ctx.SaveChanges();
            }
        }

        public bool UpdateMenuItem(int itemId, UIMenuItem updItem)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                FW_MenuItem muitem = ctx.FW_MenuItem.SingleOrDefault(c => c.ItemID == itemId);
                if (muitem != null)
                {
                    muitem.ItemName = updItem.ItemName;
                    muitem.MenuID = updItem.MenuID;
                    muitem.ItemUrl = updItem.ItemUrl;
                    muitem.MvcArea = updItem.MvcArea;
                    muitem.MvcCtrl = updItem.MvcCtrl;
                    muitem.MvcAction = updItem.MvcAction;
                    muitem.ParentID = updItem.ParentID;
                    muitem.SortNum = updItem.SortNum;
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public bool RemoveMenuItem(int itemId)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                FW_MenuItem muitem = ctx.FW_MenuItem.SingleOrDefault(c => c.ItemID == itemId);
                if (muitem != null)
                {
                    //子菜单项的ParentID设为Null
                    List<FW_MenuItem> submitemList = ctx.FW_MenuItem.Where(c => c.ParentID == itemId).ToList();
                    foreach (var submitem in submitemList)
                    {
                        submitem.ParentID = null;
                    }
                    ctx.FW_MenuItem.DeleteObject(muitem);
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public void CreateMemberMuItem(string gid, int itemId, int sortNum)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                FW_MemberMuItem memMuItem = new FW_MemberMuItem
                {
                    GID = gid,
                    MuItemID = itemId,
                    SortNum = sortNum
                };
                ctx.FW_MemberMuItem.AddObject(memMuItem);
                ctx.SaveChanges();
            }
        }

        public bool RemoveMemberMuItem(string gid, int itemId)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                FW_MemberMuItem muItem = ctx.FW_MemberMuItem.SingleOrDefault(c => c.GID.Equals(gid, StringComparison.CurrentCultureIgnoreCase) && c.MuItemID == itemId);
                if (muItem != null)
                {
                    ctx.FW_MemberMuItem.DeleteObject(muItem);
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }
    }
}
